package edu.vcu.csl.ess;

import java.util.Vector;
import java.io.*;

public class GUIData {
	public class Parameter  {public String name, value, comment;}
	public class Species    {public String name, population, comment;}
	public class Reaction   {public String name, equation, rate, comment;}
	public class Event      {public String name, time, action, comment;}
	public class Periodic   {public String name, start, period, end, action, comment;}
	public class Trigger    {public String name, condition, action, comment;}
	public class Simulation {public String name, start, interval, end, seed, outputfilename;}

	public Vector<Parameter>  parameters  = new Vector<Parameter>();
	public Vector<Species>    species     = new Vector<Species>();
	public Vector<Reaction>   reactions   = new Vector<Reaction>();
	public Vector<Event>      events      = new Vector<Event>();
	public Vector<Periodic>   periodics   = new Vector<Periodic>();
	public Vector<Trigger>    triggers    = new Vector<Trigger>();
	public Vector<Simulation> simulations = new Vector<Simulation>();

	public GUIData() {

	}

	public void write(File file) throws IOException  {
		PrintStream out = new PrintStream(new FileOutputStream(file));

		out.println("-- ESS GUI OUTPUT FILE v1.0 --");

		out.println(parameters.size());
		for(int i=0; i<parameters.size(); i++) {
			Parameter x = parameters.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.value.replace('\n','\t'));
			out.println(x.comment.replace('\n','\t'));
		}

		out.println(species.size());
		for(int i=0; i<species.size(); i++) {
			Species x = species.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.population.replace('\n','\t'));
			out.println(x.comment.replace('\n','\t'));
		}

		out.println(reactions.size());
		for(int i=0; i<reactions.size(); i++) {
			Reaction x = reactions.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.equation.replace('\n','\t'));
			out.println(x.rate.replace('\n','\t'));
			out.println(x.comment.replace('\n','\t'));
		}

		out.println(events.size());
		for(int i=0; i<events.size(); i++) {
			Event x = events.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.time.replace('\n','\t'));
			out.println(x.action.replace('\n','\t'));
			out.println(x.comment.replace('\n','\t'));
		}

		out.println(periodics.size());
		for(int i=0; i<periodics.size(); i++) {
			Periodic x = periodics.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.start.replace('\n','\t'));
			out.println(x.period.replace('\n','\t'));
			out.println(x.end.replace('\n','\t'));
			out.println(x.action.replace('\n','\t'));
			out.println(x.comment.replace('\n','\t'));
		}

		out.println(triggers.size());
		for(int i=0; i<triggers.size(); i++) {
			Trigger x = triggers.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.condition.replace('\n','\t'));
			out.println(x.action.replace('\n','\t'));
			out.println(x.comment.replace('\n','\t'));
		}

		out.println(simulations.size());
		for(int i=0; i<simulations.size(); i++) {
			Simulation x = simulations.get(i);
			out.println(x.name.replace('\n','\t'));
			out.println(x.start.replace('\n','\t'));
			out.println(x.interval.replace('\n','\t'));
			out.println(x.end.replace('\n','\t'));
			out.println(x.seed.replace('\n','\t'));
			out.println(x.outputfilename.replace('\n','\t'));
		}

		out.close();
	}

	public void read(File file) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(file));
		int count;

		if (!in.readLine().equals("-- ESS GUI OUTPUT FILE v1.0 --"))
			throw new IOException();

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addParameter(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addSpecies(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addReaction(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addEvent(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addPeriodic(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addTrigger(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}

		count = Integer.parseInt(in.readLine());
		for(int i=0; i<count; i++) {
			addSimulation(in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'),
					in.readLine().replace('\t','\n'));
		}
	}


	public void addParameter(String name,
			String value,
			String comment) {
		Parameter p = new Parameter();
		p.name = name;
		p.value = value;
		p.comment = comment;
		parameters.add(p);
	}

	public void addSpecies(String name,
			String population,
			String comment) {
		Species s = new Species();
		s.name = name;
		s.population = population;
		s.comment = comment;
		species.add(s);
	}

	public void addReaction(String name,
			String equation,
			String rate,
			String comment) {
		Reaction r = new Reaction();
		r.name = name;
		r.equation = equation;
		r.rate = rate;
		r.comment = comment;
		reactions.add(r);
	}

	public void addEvent(String name,
			String time,
			String action,
			String comment) {
		Event e = new Event();
		e.name = name;
		e.time = time;
		e.action = action;
		e.comment = comment;
		events.add(e);
	}

	public void addPeriodic(String name,
			String start,
			String period,
			String end,
			String action,
			String comment) {
		Periodic p = new Periodic();
		p.name = name;
		p.start = start;
		p.period = period;
		p.end = end;
		p.action = action;
		p.comment = comment;
		periodics.add(p);
	}

	public void addTrigger(String name,
			String condition,
			String action,
			String comment) {
		Trigger t = new Trigger();
		t.name = name;
		t.condition = condition;
		t.action = action;
		t.comment = comment;
		triggers.add(t);
	}

	public void addSimulation(String name,
			String start,
			String interval,
			String end,
			String seed,
			String outputfilename) {
		Simulation s = new Simulation();
		s.name = name;
		s.start = start;
		s.interval = interval;
		s.end = end;
		s.seed = seed;
		s.outputfilename = outputfilename;
		simulations.add(s);
	}
}